package com.sg.business.management.editor.page;

import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import com.sg.business.model.DummyBPMPackage;
import com.sg.business.model.DummyBPMProcess;
import com.sg.business.model.WFHumanTaskSetting;

public class TaskSettingContentProvider implements ITreeContentProvider {

	@Override
	public void dispose() {

	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object[] getElements(Object inputElement) {
		if (inputElement instanceof List) {
			List list = (List) inputElement;
			return list.toArray();
		} else
			return new Object[] { inputElement };
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof DummyBPMPackage) {
			DummyBPMPackage dummyBPMPackage = (DummyBPMPackage) parentElement;
			List<DummyBPMProcess> dbps = (List<DummyBPMProcess>) dummyBPMPackage
					.getValue(DummyBPMPackage.CHILD_PROCESS);
			return dbps.toArray();
		} else if (parentElement instanceof DummyBPMProcess) {
			DummyBPMProcess dummyBPMProcess = (DummyBPMProcess) parentElement;
			List<WFHumanTaskSetting> whts = (List<WFHumanTaskSetting>) dummyBPMProcess
					.getValue(DummyBPMProcess.CHILD_TASK);
			return whts.toArray();
		}
		return new Object[0];
	}

	@Override
	public Object getParent(Object element) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof DummyBPMPackage) {
			DummyBPMPackage dummyBPMPackage = (DummyBPMPackage) element;
			List<DummyBPMProcess> dbps = (List<DummyBPMProcess>) dummyBPMPackage
					.getValue(DummyBPMPackage.CHILD_PROCESS);
			if (dbps == null || dbps.isEmpty()) {
				return false;
			}
		} else if (element instanceof DummyBPMProcess) {
			DummyBPMProcess dummyBPMProcess = (DummyBPMProcess) element;
			List<WFHumanTaskSetting> whts = (List<WFHumanTaskSetting>) dummyBPMProcess
					.getValue(DummyBPMProcess.CHILD_TASK);
			if (whts == null || whts.isEmpty()) {
				return false;
			}
		} else if (element instanceof WFHumanTaskSetting) {
			return false;
		}
		return true;
	}

}
